from datetime import datetime
from typing import Optional

from pydantic import field_serializer
from sqlmodel import Field

from src.base.beans import PageParam
from src.base.constants import datetime_pattern
from src.domain.base_model import DbBaseModel
from src.utils.common_utils import serialize_datetime


class SysTenantBase(DbBaseModel):
    tenant_id: Optional[int] = Field(primary_key=True, description="租户序号", sa_column_kwargs={"comment": "租户序号"})
    tenant_name: Optional[str] = Field(description="租户名称", sa_column_kwargs={"comment": "租户名称"})
    org_name: Optional[str] = Field(description="组织名称", sa_column_kwargs={"comment": "组织名称"})
    org_credit_code: Optional[str] = Field(description="组织信用代码", sa_column_kwargs={"comment": "组织信用代码"})
    org_phone: Optional[str] = Field(description="组织电话", sa_column_kwargs={"comment": "组织电话"})
    org_intro: Optional[str] = Field(description="组织电话", sa_column_kwargs={"comment": "组织电话"})
    expire_time: Optional[datetime] = Field(description="到期时间", sa_column_kwargs={"comment": "到期时间"})
    account_count: Optional[int] = Field(description="账号数量",
                                         sa_column_kwargs={"comment": "账号数量", "server_default": "-1"})
    account_balance: Optional[float] = Field(description="余额",
                                             sa_column_kwargs={"comment": "余额", "server_default": "-1"})
    account_score: Optional[int] = Field(description="积分",
                                         sa_column_kwargs={"comment": "积分", "server_default": "-1"})

    @field_serializer("expire_time")
    def serialize_tenant_datetime(self, value: Optional[datetime], _info):
        return serialize_datetime(value, datetime_pattern)


class SysTenant(SysTenantBase, table=True):
    __tablename__ = "sys_tenant"
    __table_args__ = {'extend_existing': True, 'comment': '租户表'}


class SysTenantReq(SysTenantBase, PageParam):
    pass


class SysTenantResp(SysTenantBase):
    pass
